package com.ruoyi.training.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.training.dto.PeopleAllocationDTO;
import com.ruoyi.training.dto.TrainingClassDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.TrainingClass;
import com.ruoyi.training.service.ITrainingClassService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 培训班主Controller
 *
 * @author WorrilessGo
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/TrainingClass")
public class TrainingClassController extends BaseController
{
    @Autowired
    private ITrainingClassService trainingClassService;

    /**
     * 查询培训班主列表
     */
    @RequiresPermissions("TrainingClass:TrainingClass:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainingClass trainingClass)
    {
        startPage();
        List<TrainingClass> list = trainingClassService.selectTrainingClassList(trainingClass);
        return getDataTable(list);
    }

    /**
     * 通过项目和条件查询培训班主列表
     * lqx
     */
//    @RequiresPermissions("TrainingClass:TrainingClass:trainingClassList")
    @GetMapping("/trainingClassList")
    public TableDataInfo trainingClassList(TrainingClassDto trainingClassDto)
    {
        startPage();
        List<TrainingClassDto> list = trainingClassService.selectList(trainingClassDto);
        return getDataTable(list);
    }

    /**
     * 班主任查询我的班级列表
     * lqx
     */
    @GetMapping("/myClasses")
    public TableDataInfo myClasses( TrainingClassDto trainingClassDto) {
        // 获取当前登录用户ID
        Long currentUserId = SecurityUtils.getUserId();
        // 设置当前用户ID到DTO中
        trainingClassDto.setCurrentUserId(currentUserId.toString());
        startPage();
        return getDataTable(trainingClassService.selectMyClasses(trainingClassDto));
    }

    /**
     * 学员查询我的班级列表
     * lqx
     */
    @GetMapping("/studentClasses/{studentId}")
    public TableDataInfo studentClasses(@PathVariable("studentId") String studentId) {
        startPage();
        return getDataTable(trainingClassService.selectStudentClasses(studentId));
    }

    /**
     * 学员查询我的班级列表-后端获取id
     * lqx
     */
    @GetMapping("/studentClasses")
    public TableDataInfo studentClasses() {
        String studentId = SecurityUtils.getLoginUser().getSysUser().getStudentId();
        startPage();
        if (!"".equals(studentId) && studentId != null){
            return getDataTable(trainingClassService.selectStudentClasses(studentId));
        }else{
            return null;
        }
    }


    /**
     * 导出培训班主列表
     */
    @RequiresPermissions("TrainingClass:TrainingClass:export")
    @Log(title = "培训班主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainingClass trainingClass)
    {
        List<TrainingClass> list = trainingClassService.selectTrainingClassList(trainingClass);
        ExcelUtil<TrainingClass> util = new ExcelUtil<TrainingClass>(TrainingClass.class);
        util.exportExcel(response, list, "培训班主数据");
    }

    /**
     * 获取培训班主详细信息
     */
    @RequiresPermissions("TrainingClass:TrainingClass:query")
    @GetMapping(value = "/{trainingClassId}")
    public AjaxResult getInfo(@PathVariable("trainingClassId") String trainingClassId)
    {
        return success(trainingClassService.selectTrainingClassByTrainingClassId(trainingClassId));
    }

    /**
     * 获取培训班vo详细信息
     */
//    @RequiresPermissions("TrainingClass:TrainingClass:query")
    @GetMapping(value = "/getById/{trainingClassId}")
    public AjaxResult getInfoByTrainingClassId(@PathVariable("trainingClassId") String trainingClassId)
    {
        return success(trainingClassService.selectTrainingClassVoByTrainingClassId(trainingClassId));
    }

    /**
     * 新增培训班主
     */
    @RequiresPermissions("TrainingClass:TrainingClass:add")
    @Log(title = "培训班主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainingClass trainingClass)
    {
        return toAjax(trainingClassService.insertTrainingClass(trainingClass));
    }

    /**
     * 修改培训班主
     */
    @RequiresPermissions("TrainingClass:TrainingClass:edit")
    @Log(title = "培训班主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainingClass trainingClass)
    {
        return toAjax(trainingClassService.updateTrainingClass(trainingClass));
    }

    /**
     * 设置培训班考试
     * lqx
     */
    @PutMapping("setExam")
    public AjaxResult setExam(@RequestBody TrainingClass trainingClass)
    {
        return toAjax(trainingClassService.setExam(trainingClass));
    }

    /**
     * 通过培训班id获取培训班考试设置
     * lqx
     */
    @GetMapping(value = "/getExamInfoById/{trainingClassId}")
    public AjaxResult getExamInfoById(@PathVariable("trainingClassId") String trainingClassId)
    {
        return success(trainingClassService.getExamInfoById(trainingClassId));
    }

    /**
     * 删除培训班主
     */
    @RequiresPermissions("TrainingClass:TrainingClass:remove")
    @Log(title = "培训班主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{trainingClassIds}")
    public AjaxResult remove(@PathVariable String[] trainingClassIds)
    {
        return toAjax(trainingClassService.deleteTrainingClassByTrainingClassIds(trainingClassIds));
    }

    /**
     * 保存班级人员分配信息
     * @param allocationDTO 人员分配信息
     * @return 操作结果
     */
    @PostMapping("/save")
    public AjaxResult savePeopleAllocation(@RequestBody PeopleAllocationDTO allocationDTO) {
        return toAjax(trainingClassService.savePeopleAllocation(allocationDTO));
    }

    /**
     * 根据班级ID查询人员分配信息
     * @param trainingClassId 班级ID
     * @return 人员分配信息，已转换为原始格式
     */
    @GetMapping("/query/{trainingClassId}")
    public AjaxResult queryPeopleAllocation(@PathVariable String trainingClassId) {
        return AjaxResult.success(trainingClassService.queryPeopleAllocation(trainingClassId));
    }

    /**
     * 班级交接
     * 保存备注原因，修改班级状态
     */
    @PostMapping("/classHandover")
    public AjaxResult classHandover(@RequestBody TrainingClassDto trainingClassDto) {
        return toAjax(trainingClassService.classHandover(trainingClassDto));
    }

    /**
     * 班级交接时，查寻当前用户是否是班主任2
     */
    @GetMapping("/checkHeadTeacher2")
    public AjaxResult checkHeadTeacher2(@RequestBody TrainingClassDto trainingClassDto) {
        return toAjax(trainingClassService.classHandover(trainingClassDto));
    }

}
